const mysql = require('mysql');
const {
    mysqlConfig
} = require('../config')




const startConnect = (sql) => {

    try {
        let connection = mysql.createConnection(mysqlConfig);
        //创建连接

        connection.connect();
        return new Promise((resolve, reject) => {
            connection.query(sql, (err, res, field) => {
                if (err) {
                    throw err;
                    // reject(err);
                }
                connection.end();
                resolve(res)
            })
        }).catch(e => {
            // throw e;
            
        })

    } catch (e) {
        connection.destroy()

        connection = mysql.createConnection(mysqlConfig);
        connection.connect();

        return new Promise((resolve, reject) => {
            connection.query(sql, (e, r, f) => {
                if (e) {
                    reject(e);
                    return;
                }
                resolve(r)
            })
        })
    }


}

module.exports = startConnect;